package com.hpeng.sys.action;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hpeng.sys.model.SysTemplate;
import com.hpeng.sys.service.Function;
import com.hpeng.sys.service.LoginUserMgr;
import com.hpeng.sys.service.SysTemplateMgr;
import com.hpeng.sys.vo.Lookup;
import com.hpeng.sys.vo.FuncProperties;
import com.hpeng.util.Pagination;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Component("TemplateAction")
@Scope("prototype")
public class TemplateAction extends ActionSupport implements ModelDriven<SysTemplate> {
	public Pagination<SysTemplate> pagination;
	private int page;
	public String pageLink;	//分页的html语句
	public String navStr;	//导航条
	private String FN;
	private String accessRight;
	private String opt;
	private String msg;
	public String t;
	private String qry;
	public List<Lookup> accessRightData;
	public List<FuncProperties> sysFuncData;
	public Map<String,String> tmpltAuthMap;

	public String list() {
		this.pagination = sysTemplateMgr.getPagination(page);
		this.pageLink = pagination.getPageLink("tmplt-list.action?FN=" + FN);
		this.navStr = function.getNavStr(FN);
		this.accessRight = function.getAccessRight(request(), FN);
		
		return "list";
	}
	
	public String retrieve() throws UnsupportedEncodingException{
		qry = new String(qry.getBytes("ISO8859-1"),"UTF-8");
		this.pagination = sysTemplateMgr.getPByTmpltName(qry, page);
		this.pageLink = pagination.getPageLink("tmplt-retrieve.action?FN=" + FN + "&qry=" + qry);
		this.navStr = function.getNavStr(FN);
		this.accessRight = function.getAccessRight(request(), FN);
		return "list";
	}

	public String oprt() {
		this.navStr = function.getNavStr(FN);
		if("1".equals(opt)){
			this.sysTemplate = sysTemplateMgr.findSysTemplate(request().getParameter("t"));
		}
		return "oprt";
	}
	
	public String role() {
		this.navStr = function.getNavStr(FN);
		if("2".equals(opt)){
			this.sysFuncData = function.getFuncProperties();
			this.accessRightData = function.getLookup("ACCESS_RIGHT");
			this.tmpltAuthMap = function.getMapTemplateAuthorities(request().getParameter("t"));
			this.t = request().getParameter("t");
		}
		return "role";
	}
	
	public String setAuth(){
		try {
			String userId = loginUser.getUserId(request());
			function.modifyAuthorities(request(), userId);
		} catch (RuntimeException rx) {
			msg = getText("save.failure");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("save.success");
		}
		return role();
	}
	
	public String doOprt() {
		try{
			String userId = loginUser.getUserId(request());
			if ("0".equals(opt)) {
				if (sysTemplate.getCrtBy() == null
						|| "".equals(sysTemplate.getCrtBy())) {
					sysTemplate.setCrtBy(userId);
				}
				if (sysTemplate.getCrtDate() == null
						|| "".equals(sysTemplate.getCrtDate())) {
					sysTemplate.setCrtDate(new Date());
				}
				sysTemplate.setSeq(function.getMaxSeq("SYS_TEMPLATE_MST"));
				sysTemplateMgr.addSysTemplate(sysTemplate);
			} else {
				sysTemplate.setUpdBy(userId);
				sysTemplate.setUpdDate(new Date());
				sysTemplateMgr.updSysTemplate(sysTemplate);
			}
		} catch (RuntimeException rx) {
			msg = getText("save.failure");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("save.success");
		}

		return list();
	}
	
	public String del(){
		try {
			String t = request().getParameter("t");
			if (t != null && !"".equals(t)) {
				sysTemplateMgr.delSysTemplateByIds(t.split(","));
			}
		} catch (RuntimeException rx) {
			msg = getText("del.failure");
		}
		if (msg == null || "".equals(msg.trim())) {
			msg = getText("del.success");
		}
		return list();
	}
	
	@Resource
	private SysTemplateMgr sysTemplateMgr;
	@Resource
	private Function function;
	@Resource
	private LoginUserMgr loginUser;

	public SysTemplate sysTemplate = new SysTemplate();
	public SysTemplate getModel() {
		return sysTemplate;
	}
	public HttpServletRequest request(){
		return ServletActionContext.getRequest();
	}
	public Map<String,Object> session(){
		return ActionContext.getContext().getSession();
	}
	public String getFN() {
		return FN;
	}
	public void setFN(String FN) {
		this.FN = FN;
	}
	public String getAccessRight() {
		return accessRight;
	}
	public void setAccessRight(String accessRight) {
		this.accessRight = accessRight;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public String getOpt() {
		return opt;
	}
	public void setOpt(String opt) {
		this.opt = opt;
	}
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public String getT() {
		return t;
	}
	public void setT(String t) {
		this.t = t;
	}
	public String getQry() {
		return qry;
	}
	public void setQry(String qry) {
		this.qry = qry;
	}
}
